﻿using System.Collections.ObjectModel;
using System.Windows;
using treeView.InterfaceWindow;

namespace treeView
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            DataContext = this;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Initialize();
        }

        private void Initialize()
        {
            Machine.Instance.ReadConfig();
            Machine.Instance.ConncetConfigDb();
            try
            {
                Machine.Instance.Connect();

            }
            catch (Exception ex)
            {

                MessageBox.Show("连接失败", ex.ToString());
            }

            Machine.Instance.StartUpdate();
            foreach (var item in RootNodes)
            {
                ceshi_treeView.Items.Add(item);
            }
        }

        private PLCInterface window1;
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            if (window1 == null)
            {
                window1 = new PLCInterface();
                window1.Closed += (s, e) => window1 = null; // 当窗口关闭时重置实例
                window1.Topmost = true;
                window1.Show();
            }
            else
            {
                window1.WindowState = WindowState.Normal;
                window1.Activate();
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            var td = Machine.Instance.GetPlcByIndex(0).ReadData("ns=3;s=\"HMI交互1\".\"Bias2操作\".\"断开连接\"");

            txb_ceshi.Text = td.ToString();
        }
        public ObservableCollection<TreeNode> RootNodes { get; set; }=
        new ObservableCollection<TreeNode>
        {
            new TreeNode
            {
                NodeName = "节点 1",
                ImagePath = "pack://application:,,,/treeView;component/Images/根节点.png",
                Children = new ObservableCollection<TreeNode>()
                {
                    new TreeNode { NodeName = "子节点 1-1", ImagePath = "pack://application:,,,/treeView;component/Images/子节点.png"},
                    new TreeNode { NodeName = "子节点 1-2", ImagePath = "pack://application:,,,/treeView;component/Images/子节点.png"}
                }
            },
            new TreeNode
            {
                NodeName = "节点 2",
                ImagePath = "pack://application:,,,/treeView;component/Images/根节点.png",
                Children =new ObservableCollection<TreeNode>()
                {
                    new TreeNode { NodeName = "子节点 2-1", ImagePath = "pack://application:,,,/treeView;component/Images/子节点.png"}
                }
            }
        };
    }

    //public class TreeNode
    //{
    //    public string NodeName { get; set; }
    //    public string ImagePath { get; set; } // 图片文件的路径
    //    public ObservableCollection<TreeNode> Children { get; set; }

    //    public TreeNode()
    //    {
    //        Children = new ObservableCollection<TreeNode>();
    //    }
    //}
}